package ptij.vocoder.baseModel;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.BooleanToken;
import ptolemy.data.Token;
import ptolemy.data.type.*;
import ptolemy.data.expr.Parameter;

//////////////////////////////////////////////////////////////////////////
//// TokenDelay
/** 

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class TokenDelay extends TypedAtomicActor {

    public TokenDelay(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        input = new TypedIOPort(this, "input", true, false);

        reset = new TypedIOPort(this, "reset", true, false);

        output = new TypedIOPort(this, "output", false, true);
        
        initialToken = new Parameter(this,"initialToken", new BooleanToken(true));
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports
    public TypedIOPort input;
    public TypedIOPort reset;

    // Output Ports
    public TypedIOPort output;
    
    public Parameter initialToken;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
        _init();
    }

    public void fire() throws IllegalActionException {
        if(reset.hasToken(0)) {
            reset.get(0);
            _init();
        }

        output.broadcast(_token);

        if(input.hasToken(0)) {
            _token = input.get(0);
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////


    private void _init() throws IllegalActionException {
        _token = initialToken.getToken();
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private Token _token;
}
